package com.smartstate.admin.biz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.smartstate.admin.biz.entity.dto.RolePermissionDto;
import com.smartstate.admin.biz.entity.dto.RoleDto;
import com.smartstate.admin.biz.entity.dto.RoleSearchDto;
import com.smartstate.admin.biz.entity.vo.RoleVo;
import com.smartstate.admin.biz.service.PermissionService;
import com.smartstate.admin.biz.service.RoleService;
import com.smartstate.common.base.dto.Parameter;
import com.smartstate.common.base.httpResult.ResultCode;
import com.smartstate.common.base.httpResult.ResultVO;
import com.smartstate.common.base.httpResult.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author: runner-up
 * @Package: com.smartstate.admin.biz.controller
 * @ClassName: RoleController
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController extends BaseController {

    @Resource
    private RoleService roleService;

    @Resource
    private PermissionService permissionService;

    @GetMapping("/api/info/{id}")
    @ApiOperation(value = "根据用户id获取用户角色信息")
    public ResultVO getRoleByUserId(@PathVariable String id) {
        return ResultVO.success(roleService.getRoleByUserId(id));
    }

    @GetMapping("/web/list")
    @ApiOperation(value = "查询角色管理列表--分页", notes = "查询角色管理列表--分页")
    public ResultVO getRolePage(@ModelAttribute RoleSearchDto params){
        return ResultVO.success(roleService.queryRoleList(params));
    }

    @GetMapping("/web/select/list")
    @ApiOperation(value = "查询不属于该用户的角色", notes = "查询不属于该用户的角色")
    @Deprecated
    public ResultVO getRoleList(@RequestParam(required = false) String userId){
        return ResultVO.success(roleService.queryRole(userId));
    }

    @GetMapping(value = "/web/info/{id}")
    @ApiOperation(value = "获取角色详情", notes = "获取角色详情")
    public ResultVO getRole(@ApiParam(value = "角色id",required = true)@PathVariable String id){
        RoleVo role = roleService.queryRoleById(id);
        if (null == role){
            return ResultVO.error("未查找到该角色");
        }else{
            return ResultVO.success(role);
        }
    }

    //@SysLog("新增角色")
    @PostMapping(value = "/web/save")
    @ApiOperation(value = "新增角色", notes = "新增角色")
    public ResultVO saveRole(@RequestBody RoleDto roleDto){
        Integer integer = roleService.saveOrUpdateRole(roleDto);
        if (integer == 1){
            return ResultVO.success();
        }else {
            return ResultVO.error(ResultCode.COMMON_FAIL);
        }
    }

    //@SysLog("修改角色")
    @PutMapping(value = "/web/update")
    @ApiOperation(value = "修改角色管理", notes = "修改角色管理")
    public ResultVO updateRole(@RequestBody RoleDto roleDto){
        Integer integer = roleService.saveOrUpdateRole(roleDto);
        if (integer == 1){
            return ResultVO.success();
        }else {
            return ResultVO.error(ResultCode.COMMON_FAIL);
        }
    }

    @GetMapping(value = "/web/checkRole")
    @ApiOperation(value = "校验角色某属性值是否重复", notes = "校验角色某属性值是否重复")
    public ResultVO checkUserName(@ApiParam(value = "角色编号", required = false) @RequestParam(required = false) String roleCode
            , @ApiParam(value = "角色名称", required = false) @RequestParam(required = false) String roleName
            ,@ApiParam(value="角色Id",required = false) @RequestParam(required = false) String roleId) {
        Map<String, Object> map = null;
        boolean flag = false;
        if (StringUtils.isNotBlank(roleCode)){
            map = roleService.checkRoleCode(roleCode, roleId);
            flag = (boolean) map.get("flag");
        }
        if (StringUtils.isNotBlank(roleName)){
            map = roleService.checkRoleName(roleName, roleId);
            flag = (boolean) map.get("flag");
        }
        if (flag) {
            return ResultVO.success("成功");
        } else {
            String msg = (String) map.get("msg");
            return ResultVO.error(msg);
        }
    }

    //@SysLog("删除角色")
    @DeleteMapping(value = "/web/del/{id}")
    @ApiOperation(value = "删除角色管理", notes = "删除角色管理")
    public ResultVO deleteRole(@ApiParam(value = "角色id",required = true)@PathVariable String id){
        Map<String, Object> map = roleService.deleteRole(id);
        boolean flag = (boolean) map.get("flag");
        String msg = (String) map.get("msg");
        if (flag){
            return ResultVO.success(msg);
        }else {
            return ResultVO.error(msg);
        }
    }


    //@SysLog("批量删除角色")
    @DeleteMapping(value = "/web/delBatch")
    @ApiOperation(value = "批量删除角色管理", notes = "批量删除角色管理")
    public ResultVO deleteBatchRole(@RequestBody String ids){
        if (StringUtils.isBlank(ids)) {
            return ResultVO.error("参数不能为空");
        }
        List<String> idList = JSON.parseArray(((JSONArray) JSON.parseObject(ids).get("ids")).toJSONString(), String.class);
        Map<String, Object> map = roleService.deleteBatchRole(idList);
        boolean flag = (boolean) map.get("flag");
        String msg = (String) map.get("msg");
        if (flag){
            return ResultVO.success(msg);
        }else {
            return ResultVO.error(msg);
        }
    }

    @GetMapping("/web/permission/{id}")
    @ApiOperation("获取该角色拥有的web权限")
    public ResultVO getPermissionByRoleId(@ApiParam("角色id") @PathVariable String id){
        if (StringUtils.isBlank(id)) {
            return ResultVO.error("id不能为空");
        }
        return ResultVO.success(permissionService.getPermissionByRoleId(id));
    }

    @GetMapping("/app/permission/{id}")
    @ApiOperation("获取该角色拥有的app权限")
    public ResultVO getAppPermissionByRoleId(@ApiParam("角色id") @PathVariable String id){
        if (StringUtils.isBlank(id)) {
            return ResultVO.error("id不能为空");
        }
        return ResultVO.success(permissionService.getAppPermissionByRoleId(id));
    }


    @PostMapping("/web/permission/save/{id}")
    @ApiOperation("分配该角色选择的权限")
    public ResultVO savePermissionAndRole(@ApiParam("角色id") @PathVariable String id, @RequestBody Parameter<List<RolePermissionDto>> rolePermissionDtos){
        if (StringUtils.isBlank(id)) {
            return ResultVO.error("角色id不能为空");
        }
        if (permissionService.savePermissionAndRole(id,rolePermissionDtos.getData())){
            return ResultVO.success("分配成功");
        }else {
            return ResultVO.error("分配失败");
        }
    }




}
